//app.js
const {
  checkLogin
} = require('./utils/util')
const {
  decodeUserInfo,
} = require('./api/login')
App({
  onLaunch: function () {
    this.getSystemInfo()
    this.checkStatus()
  },
  editTabbar: function () {
    const type = this.globalData.type
    let tabbar = this.globalData.tabbar
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    let pagePath = _this.route;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
  },
  async fetchUserInfo() {
    const {
      result
    } = await decodeUserInfo()
  },
  checkStatus() {
    const _self = this
    checkLogin('none').then((res) => {
      _self.globalData.isLogin = true
      _self.fetchUserInfo()
    })
  },
  getSystemInfo: function () {
    let t = this;
    let menuButtonObject = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: function (res) {
        let navTop = menuButtonObject.top;
        let menuHeight = menuButtonObject.height
        let navHeight = menuButtonObject.top + menuButtonObject.height
        t.globalData.navHeight = navHeight,
          t.globalData.menuHeight = menuHeight,
          t.globalData.navTop = navTop,
          t.globalData.systemInfo = res;
        const {
          screenHeight,
          windowHeight,
          safeArea
        } = res
        const {
          bottom
        } = safeArea
        const bottomLift = screenHeight - bottom
        const realWindowHeight = (windowHeight - navHeight - 10) * 2
        t.globalData.windowHeight = realWindowHeight
        t.globalData.bottomLift = bottomLift
        const tabbarHeight = screenHeight - res.windowHeight
        t.globalData.mainHeight = screenHeight - t.globalData.navHeight;
        t.globalData.tabbarHeight = tabbarHeight
      }
    });
  },
  globalData: {
    tabbarHeight: 48,
    bottomLift: 0,
    navHeight: 0,
    navTop: 0,
    menuHeight: 0,
    isLogin: false,
    windowHeight: 0,
    showRole: false,
    tabbar: {
      "backgroundColor": "#FFFFFF",
      "color": "#333333",
      "selectedColor": "#2069EB",
      "borderStyle": "white",
      "list": [{
          "pagePath": "/pages/index/index",
          "text": "首页",
          "iconPath": "/images/tabbar/tab_icon1.png",
          "selectedIconPath": "/images/tabbar/tab_icon1a.png"
        },
        // {
        //   "pagePath": "/pages/notice/index",
        //   "text": "报警",
        //   "iconPath": "/images/tabbar/tab_icon2.png",
        //   "selectedIconPath": "/images/tabbar/tab_icon2a.png"
        // },
        {
          "pagePath": "/pages/my/index",
          "text": "个人中心",
          "iconPath": "/images/tabbar/my.png",
          "selectedIconPath": "/images/tabbar/my_a.png"
        }
      ]
    },
  }
})